/*
 * ZENworks Notification Icon
 * Copyright (C) Author: Gangadhar Nagesh Metla (Novell, Inc.)
 * nmetla74@gmail.com 
 * Version 1.0
 * 1/3/2009
 * Filename ZActivityController.java
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.novell.zenworks.agent.zicon.progress;

import java.util.HashMap;
import java.util.Map;

import com.novell.zenworks.agent.zicon.ZTrayIcon;
import com.novell.zenworks.zicon.common.progress.ITaskSetProgress.ProgressStatus;

public class ZActivityController 
{
    private static ZActivityController _instance = null;
    private Map<String, ZActivityLauncher> activityDlgMap = new HashMap<String, ZActivityLauncher>();
    private static Object _lock = new Object();

    private ZActivityController()
    {}
    
    public static ZActivityController getInstance()
    {
        if(_instance == null)
        {
            synchronized(_lock)
            {
                if(_instance == null)
                    _instance = new ZActivityController();
            }
        }
        return _instance;
    }

    public boolean launchActivityDialog(String guid, ProgressStatus appStatus, String bundleName)
    {
        ZActivityData data = getActivityData(appStatus, bundleName);

        try
        {
            if (!activityDlgMap.containsKey(guid))
            {
                ZActivityLauncher activity = new ZActivityLauncher(data);
                activityDlgMap.put(guid, activity);
                Thread t = new Thread(activity);
                t.start();

            }
            else
            {
                updateActivityData(guid, appStatus, appStatus, bundleName);
            }
        }
        catch (Exception e)
        {
            return false;
        }
        return true;
    }

    public boolean updateActivityData(String guid, ProgressStatus appAction, ProgressStatus appStatus, String bundleName)
    {
        ZActivityData data = getActivityData(appAction, appStatus, bundleName);

        try
        {
            if (activityDlgMap.containsKey(guid))
            {
                ZActivityLauncher activity = (ZActivityLauncher)activityDlgMap.get(guid);
                updateActivityData(activity, data);
            }
            else
            {
                launchActivityDialog(guid, appStatus, bundleName);
            }
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }


    private void updateActivityData(ZActivityLauncher activity, ZActivityData data)
    {
        activity.setActivityName(data.getName());
        activity.setProgressUICurrentAction(data.getProgressUICurrentAction());
        activity.setProgressUIHeading(data.getProgressUIHeading());
        activity.getDialog().validate();
    }

    public boolean disposeActivityDialog(String guid)
    {
        boolean retVal = false;
        ZActivityLauncher activity = (ZActivityLauncher)activityDlgMap.get(guid);

        if(activity != null)
        {
            activity.getDialog().dispose();
            activityDlgMap.remove(guid);
            retVal = true;
        }
        return retVal;
    }

    // Utility methods
    public static ZActivityData getActivityData(ProgressStatus appStatus, String bundleName)
    {
        String status = getDisplayString(appStatus);

        return new ZActivityData(status + "...", status +" " + ZTrayIcon.rm.getString("Application"), bundleName);
    }

    private ZActivityData getActivityData(ProgressStatus appAction, ProgressStatus appStatus, String bundleName)
    {
        String status = getDisplayString(appStatus);
        String action = getDisplayString(appAction);

        return new ZActivityData(action + "...", status + " " + ZTrayIcon.rm.getString("Application"), bundleName);

    }


    private static String getDisplayString(ProgressStatus appStatus)
    {
        String status = null;
        switch (appStatus)
        {
            case Downloading:
            {
                status = ZTrayIcon.rm.getString("ProgressStatusDownloading");
                break;
            }
            case Installing:
            {
                status = ZTrayIcon.rm.getString("ProgressStatusInstalling");
                break;
            }
            case Uninstalling:
            {
                status = ZTrayIcon.rm.getString("ProgressStatusUninstalling");
                break;
            }
            case ScanningInventory:
            {
                status = ZTrayIcon.rm.getString("ProgressStatusScanningInventory");
                break;
            }
            case Launching:
            {
                status = ZTrayIcon.rm.getString("ProgressStatusLaunching");
                break;
            }
            case Verifying:
            {
                status =  ZTrayIcon.rm.getString("ProgressStatusVerifying");
                break;
            }
            case Terminating:
            {
                status = ZTrayIcon.rm.getString("ProgressStatusTerminating");
                break;
            }
            case Finished:
            {
                status = ZTrayIcon.rm.getString("ProgressStatusFinished");
                break;
            }
            case Aborted:
            {
                status = ZTrayIcon.rm.getString("ProgressStatusAborted");
                break;
            }            
            default:
            {
                status = "";   
            }
        }
        return status;
    }

}
